//
//  KWTPlantGroupCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/15.
//

#import "KWTPlantGroupCell.h"
#import "RHTools.h"

@interface KWTPlantGroupCell()


@property (nonatomic ,strong) QMUILabel *plantGroupName;

@property (nonatomic ,strong) QMUILabel *plantTypeLab;//站点类型

@property (nonatomic ,strong) QMUILabel *totalPlantLab;//总站点数量

@property (nonatomic ,strong) QMUILabel *powerLab;//功率

@property (nonatomic ,strong) QMUILabel *todayGenerationLab;//今日发电量

//@property (nonatomic ,strong) QMUILabel *descriteLab;


@end
@implementation KWTPlantGroupCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(KWTPlantGroupModel *)model
{
    _model = model;
    self.plantGroupName.text = model.groupName;
    self.plantTypeLab.text = model.type;
    
    //今日发电量
    NSString *gen_t = [NSDecimalNumber decimalStringWithDoubleString:model.todayElectricityGeneration nullStr:@""];
    //电池功率
    NSString *power = [NSDecimalNumber decimalStringWithDoubleString:model.power nullStr:@""];
    
    self.totalPlantLab.text = [NSString stringWithFormat:@"%@/%@",ValidNullDataToString(model.plantNormalCount, @"--"),ValidNullDataToString(model.plantCount, @"--")];

    self.todayGenerationLab.text = gen_t;
    self.powerLab.text = power;
}

- (void)setupUI{

    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    kWeakSelf;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    
    
    _plantGroupName = [[QMUILabel alloc] init];
    [_plantGroupName setFont:[UIFont boldSystemFontOfSize:16]];
    _plantGroupName.adjustsFontSizeToFitWidth = YES;
    _plantGroupName.minimumScaleFactor = 0.8;
    _plantGroupName.numberOfLines = 0;
    [_plantGroupName setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_plantGroupName];
    [_plantGroupName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
    }];
    
    
    QMUIButton *moreBtn = [[QMUIButton alloc]init];
    [moreBtn setImage:[UIImage imageNamed:@"vpp_set_more"] forState:UIControlStateNormal];
    [bgView addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.right.mas_offset(35);
        make.left.equalTo(weakSelf.plantGroupName.mas_right).offset(5);
        make.right.mas_offset(-15);
        make.centerY.equalTo(weakSelf.plantGroupName);
    }];
    
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.equalTo(weakSelf.plantGroupName.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    
    //卡片
    UIView *bgView2 = kView;
    bgView2.backgroundColor = CFTabViewBgColor;
    [bgView2 rounded:8];
    [bgView addSubview:bgView2];
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(line.mas_bottom).offset(15);
        make.height.offset(80);
    }];
    
    UIImageView *bgImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_group_bgImg"]];
    [bgView2 addSubview:bgImg];
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    UIImageView *line_vertical = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_group_line"]];;
    [bgView2 addSubview:line_vertical];
    [line_vertical mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(0);
        make.height.mas_offset(42);
        make.width.mas_offset(1);
    }];
    
    UIView *leftView = kView;
    [bgView2 addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.equalTo(line_vertical.mas_left);
        make.centerY.mas_offset(0);
    }];
    
    _todayGenerationLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(20) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    [leftView addSubview:_todayGenerationLab];
    [_todayGenerationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.top.mas_offset(0);
    }];
    
    QMUILabel *totalTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentCenter];
    totalTitleLab.numberOfLines = 2;
    totalTitleLab.text = @"今日发电量".rh_localized;
    [leftView addSubview:totalTitleLab];
    [totalTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.todayGenerationLab);
        make.top.equalTo(weakSelf.todayGenerationLab.mas_bottom).offset(10);
        make.bottom.mas_offset(0);
    }];
    
    
    UIView *rightView = kView;
    [bgView2 addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line_vertical.mas_right);
        make.right.mas_offset(0);
        make.centerY.mas_offset(0);
    }];
    _powerLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(20) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    [rightView addSubview:_powerLab];
    [_powerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
    }];
    
    QMUILabel *powerTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentCenter];
    powerTitleLab.numberOfLines = 2;
    powerTitleLab.text = @"电池功率".rh_localized;
    [rightView addSubview:powerTitleLab];
    [powerTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.powerLab);
        make.top.equalTo(weakSelf.powerLab.mas_bottom).offset(10);
        make.bottom.mas_offset(0);
    }];
    
    //站点类型
    QMUILabel *stationTypeTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8) textAligment:NSTextAlignmentLeft];
    stationTypeTitleLab.text = @"站点类型".rh_localized;
    [bgView addSubview:stationTypeTitleLab];
    [stationTypeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(bgView2.mas_bottom).offset(15);
        
    }];

    _plantTypeLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 1) textAligment:NSTextAlignmentRight];
    _plantTypeLab.text = @"--".rh_localized;
    [bgView addSubview:_plantTypeLab];
    [_plantTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.left.equalTo(stationTypeTitleLab.mas_right).offset(5);
        make.top.equalTo(stationTypeTitleLab);
    }];
  
    //站点数量
    QMUILabel *stationCountTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8) textAligment:NSTextAlignmentLeft];
    stationCountTitleLab.text = @"站点(正常/总数):".rh_localized;
    [bgView addSubview:stationCountTitleLab];
    [stationCountTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(stationTypeTitleLab.mas_bottom).offset(15);
        
    }];
    
    _totalPlantLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 1) textAligment:NSTextAlignmentRight];
    _totalPlantLab.text = @"--".rh_localized;
    [bgView addSubview:_totalPlantLab];
    [_totalPlantLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(stationCountTitleLab);
    }];
    
    
//    
//    _descriteLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8) textAligment:NSTextAlignmentLeft];
//    [bgView addSubview:_descriteLab];
    
    
    QMUIButton *chgBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [chgBtn setImagePosition:QMUIButtonImagePositionLeft];
//    [setBtn setImage:[UIImage imageNamed:@"vpp_set_chg"] forState:UIControlStateNormal];
    [chgBtn setTitle:@"强制充电".rh_localized forState:UIControlStateNormal];
    [chgBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [chgBtn setTitleColor:kCOLOR_HEX(@"#566480") forState:UIControlStateNormal];
//    [chgBtn setSpacingBetweenImageAndTitle:5];
    [chgBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    chgBtn.titleLabel.numberOfLines = 2;
    [chgBtn rounded:8 width:1 color:kCOLOR_HEX(@"#E3E6ED")];
    [chgBtn setBackgroundColor:kCOLOR_HEX_A(@"#F4F6F9",1)];
    [bgView addSubview:chgBtn];
    
    
    QMUIButton *dchgBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [dchgBtn setImagePosition:QMUIButtonImagePositionLeft];
//    [editBtn setImage:[UIImage imageNamed:@"vpp_set_dchg"] forState:UIControlStateNormal];
    [dchgBtn setTitle:@"强制放电".rh_localized forState:UIControlStateNormal];
    [dchgBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [dchgBtn setTitleColor:kCOLOR_HEX(@"#566480") forState:UIControlStateNormal];
    [dchgBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    dchgBtn.titleLabel.numberOfLines = 2;
    [dchgBtn rounded:8 width:1 color:kCOLOR_HEX(@"#E3E6ED")];
    [dchgBtn setBackgroundColor:kCOLOR_HEX_A(@"#F4F6F9",1)];
    [bgView addSubview:dchgBtn];
    
    
    QMUIButton *standbyBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [standbyBtn setImagePosition:QMUIButtonImagePositionLeft];
//    [editBtn setImage:[UIImage imageNamed:@"vpp_set_dchg"] forState:UIControlStateNormal];
    [standbyBtn setTitle:@"待机".rh_localized forState:UIControlStateNormal];
    [standbyBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [standbyBtn setTitleColor:kCOLOR_HEX(@"#566480") forState:UIControlStateNormal];
    [standbyBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    standbyBtn.titleLabel.numberOfLines = 2;
    [standbyBtn rounded:8 width:1 color:kCOLOR_HEX(@"#E3E6ED")];
    [standbyBtn setBackgroundColor:kCOLOR_HEX_A(@"#F4F6F9",1)];
    [bgView addSubview:standbyBtn];
   
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];

 
//    [_descriteLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_offset(15);
//        make.right.mas_offset(-15);
//        make.top.equalTo(bgView2.mas_bottom).offset(15);
//    }];
    
    CGFloat standbyBtnWidth = (kUI_WINDOW_W-80)/3 - 30;
    
    [standbyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stationCountTitleLab.mas_bottom).offset(15);
        make.right.offset(-15);
        make.height.offset(40);
        make.width.offset(standbyBtnWidth);
        make.bottom.mas_offset(-10);
    }];
    
    [dchgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(chgBtn);
        make.right.equalTo(standbyBtn.mas_left).offset(-10);
        make.height.offset(40);
        
    }];
    
    [chgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stationCountTitleLab.mas_bottom).offset(15);
        make.right.equalTo(dchgBtn.mas_left).offset(-10);
        make.height.offset(40);
        make.width.equalTo(dchgBtn);
        make.left.mas_offset(15);
        make.bottom.mas_offset(-10);
    }];
    
    
    
    [[moreBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.moreBlock) {
           CGRect rect = [weakSelf.contentView convertRect:moreBtn.frame toView:kAppWindow];
            weakSelf.moreBlock(weakSelf.model,rect);
        }
    }];
    
    [[chgBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.chgBlock) {
            weakSelf.chgBlock(weakSelf.model);
        }
    }];
    
    [[dchgBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.dchgBlock) {
            weakSelf.dchgBlock(weakSelf.model);
        }
    }];
    
    [[standbyBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.standbyBlock) {
            weakSelf.standbyBlock(weakSelf.model);
        }
    }];
}

@end
